<template>
  <div class="box">
    <div class="green">
      <header>
        <i class="el-icon-arrow-left" @click="$router.go(-1)"></i>
        <div>个人资料</div>
        <div><p @click="save">保存</p></div>
      </header>
      <div class="imginfo">
        <van-uploader :after-read="afterRead">
          <img :src="$store.state.user.userimg" alt="" />
        </van-uploader>
      </div>
    </div>
    <div class="userinfo">
      <p><span>账号:</span><input type="text" v-model="user" /></p>
      <p><span>介绍:</span><input type="text" v-model="info" /></p>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import { Toast } from "vant";
Vue.use(Toast);
export default {
  data() {
    return {
      user: this.$store.state.user.username,
      info: this.$store.state.user.userinfo
    };
  },
  watch: {
    img() {
      console.log(11);
    },
  },
  methods: {
    afterRead(file) {
      let params = new FormData();
      params.append("file", file.file);
      let config = {
        headers: {
          //添加请求头
          "Content-Type": "multipart/form-data",
        },
      };
      this.$store.dispatch("upload", params, config).then((res) => {
        if (res.data.code == 200) {
          Toast.success("修改成功");
        } else {
          Toast.fail("修改失败");
        }
      });
    },
    save() {
      this.$store
        .dispatch("changeUser", {
          username: this.user,
          info: this.info,
          img: this.$store.state.user.userimg,
        })
        .then((res) => {
          if (res.status == 200) {
            Toast.success("保存成功");
          } else {
            Toast.fail("保存失败");
          }
        });
    },
  },
};
</script>

<style scoped>
.box {
  height: 100%;
  background-color: #ffffff;
}
.green {
  color: #ffffff;
  background-color: #33b17b;
}
header {
  height: 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 0.22rem;
  box-sizing: border-box;
}
header i {
  font-size: 0.48rem;
  flex: 1;
}
header div {
  font-size: 0.32rem;
  flex: 1;
  text-align: center;
}
header div:nth-of-type(2) {
  text-align: right;
}
header p {
  display: inline-block;
}
.imginfo {
  height: 2.88rem;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 0.3rem;
  box-sizing: border-box;
}
.imginfo img {
  width: 1.54rem;
  height: 1.54rem;
  border-radius: 50%;
  border: 1px solid #2c996b;
}
.userinfo {
  padding: 0 0.3rem;
}
.userinfo p {
  height: 0.98rem;
  display: flex;
  align-items: center;
  font-size: 0.28rem;
  border-bottom: 1px solid rgba(153, 153, 153, 0.1);
}
.userinfo p:last-of-type {
  border: 0;
}
.userinfo input {
  flex: 1;
  height: 0.7rem;
  color: #99a4bd;
  margin-left: 0.2rem;
  font-size: 0.28rem;
}
</style>
